<template>
  <view class="change-password-container">
    <view class="header">
      <view class="back" @tap="navigateBack">
        <text class="icon">←</text>
      </view>
      <text class="title">修改密码</text>
      <view class="placeholder"></view>
    </view>

    <view class="form-container">
      <view class="form-item">
        <text class="label">当前密码</text>
        <view class="password-input">
          <input class="input" type="password" placeholder="请输入当前密码" v-model="currentPassword" :password="!showCurrentPassword" />
          <text class="toggle-password" @tap="showCurrentPassword = !showCurrentPassword">
            {{ showCurrentPassword ? '隐藏' : '显示' }}
          </text>
        </view>
      </view>

      <view class="form-item">
        <text class="label">新密码</text>
        <view class="password-input">
          <input class="input" type="password" placeholder="请设置6-20位新密码" v-model="newPassword" :password="!showNewPassword" />
          <text class="toggle-password" @tap="showNewPassword = !showNewPassword">
            {{ showNewPassword ? '隐藏' : '显示' }}
          </text>
        </view>
        <text class="tips">密码需包含字母和数字，长度6-20位</text>
      </view>

      <view class="form-item">
        <text class="label">确认新密码</text>
        <input class="input" type="password" placeholder="请再次输入新密码" v-model="confirmNewPassword" :password="!showConfirmNewPassword" />
      </view>

      <button class="confirm-btn" @tap="changePassword" :disabled="!currentPassword || !newPassword || !confirmNewPassword || newPassword !== confirmNewPassword">确认修改</button>
    </view>
  </view>
</template>

<script>
import { request } from '@/utils/request.js';

export default {
  data() {
    return {
      currentPassword: '',
      newPassword: '',
      confirmNewPassword: '',
      showCurrentPassword: false,
      showNewPassword: false,
      showConfirmNewPassword: false
    };
  },
  methods: {
    // 返回上一页
    navigateBack() {
      uni.navigateBack();
    },

    // 修改密码
    async changePassword() {
      if (!this.currentPassword) {
        uni.showToast({ title: '请输入当前密码', icon: 'none' });
        return;
      }

      if (!this.newPassword) {
        uni.showToast({ title: '请设置新密码', icon: 'none' });
        return;
      }

      if (this.newPassword.length < 6 || this.newPassword.length > 20) {
        uni.showToast({ title: '密码长度应为6-20位', icon: 'none' });
        return;
      }

      // 密码强度校验
      const hasLetter = /[a-zA-Z]/.test(this.newPassword);
      const hasNumber = /\d/.test(this.newPassword);
      if (!hasLetter || !hasNumber) {
        uni.showToast({ title: '密码需包含字母和数字', icon: 'none' });
        return;
      }

      if (this.newPassword !== this.confirmNewPassword) {
        uni.showToast({ title: '两次输入的新密码不一致', icon: 'none' });
        return;
      }

      if (this.currentPassword === this.newPassword) {
        uni.showToast({ title: '新密码不能与当前密码相同', icon: 'none' });
        return;
      }

      try {
        // 调用修改密码API
        const res = await request({
          url: 'user/changePassword',
          data: {
            currentPassword: this.currentPassword,
            newPassword: this.newPassword,
            confirmNewPassword: this.confirmNewPassword
          },
          showLoading: true
        });

        if (res.code === 200) {
          uni.showToast({ title: '密码修改成功', icon: 'success' });

          // 清除表单
          this.currentPassword = '';
          this.newPassword = '';
          this.confirmNewPassword = '';

          // 返回上一页
          setTimeout(() => {
            uni.navigateBack();
          }, 1500);
        } else {
          uni.showToast({ title: res.msg || '密码修改失败', icon: 'none' });
        }
      } catch (error) {
        console.error('修改密码失败:', error);
        uni.showToast({ title: '密码修改失败，请重试', icon: 'none' });
      }
    }
  }
};
</script>

<style scoped>
.change-password-container {
  padding: 0;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background-color: #f8f8f8;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 88rpx;
  padding: 0 30rpx;
  background-color: #fff;
  border-bottom: 1rpx solid #e5e5e5;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 99;
}

.back {
  width: 40rpx;
  height: 40rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon {
  font-size: 32rpx;
  color: #333;
}

.title {
  font-size: 36rpx;
  color: #333;
  font-weight: 500;
}

.placeholder {
  width: 40rpx;
}

.form-container {
  padding: 40rpx;
  margin-top: 100rpx;
  flex: 1;
}

.form-item {
  margin-bottom: 30rpx;
}

.label {
  font-size: 28rpx;
  color: #666;
  margin-bottom: 10rpx;
  display: block;
}

.input {
  width: 100%;
  height: 90rpx;
  background-color: #fff;
  border-radius: 10rpx;
  padding: 0 30rpx;
  font-size: 32rpx;
  box-sizing: border-box;
  border: 1rpx solid #e5e5e5;
}

.password-input {
  position: relative;
  width: 100%;
}

.toggle-password {
  position: absolute;
  right: 30rpx;
  top: 50%;
  transform: translateY(-50%);
  font-size: 28rpx;
  color: #409eff;
}

.tips {
  font-size: 24rpx;
  color: #999;
  margin-top: 8rpx;
  display: block;
}

.confirm-btn {
  width: 100%;
  height: 90rpx;
  background-color: #409eff;
  color: #fff;
  font-size: 32rpx;
  border-radius: 10rpx;
  margin-top: 60rpx;
}

.confirm-btn:disabled {
  background-color: #ccc;
}
</style>
